package leetcode;

import java.util.Arrays;
import java.util.Stack;

public class onethousandandfortyseven {
    public static String removeDuplicates(String s) {
        Stack<Character> Stack=new Stack<>();
        Stack.push(s.charAt(0));
        for(int i=1;i<s.length();i++){
            if(Stack.isEmpty()){
                Stack.push(s.charAt(i));
                continue;
            }
           if(s.charAt(i)==Stack.peek()){
                Stack.pop();
            }else{
               Stack.push(s.charAt(i));
           }
        }
        String m="";
        while(!Stack.isEmpty()){
            char n=Stack.pop();
            m=m+n;
        }
        String reversed=new StringBuilder(m).reverse().toString();
        return reversed;
    }

    public static void main(String[] args) {
        String s="abbaca";
        String s1 = removeDuplicates(s);
        System.out.println(s1);

    }
}
